//
//  RBProfileVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBProfileVC.h"
#import <UIColor+YYAdd.h>
#import "RBThemeConfig.h"
#import <UIControl+YYAdd.h>
#import "UIView+RBAdd.h"
#import "RBDefine.h"
#import <Masonry.h>
#import "UIButton+RBAdd.h"
#import "RBProfileVC.h"
#import "UIScrollView+RBAdd.h"
#import "RBChangePhoneVC.h"
#import "RBChangeNickNameVC.h"
#import "RBSecurityVC.h"
#import "RBProfileResponse.h"
#import "UIViewController+RBAdd.h"
#import "UIImageView+RBAdd.h"
#import "RBUploadResponse.h"
#import <UIView+YYAdd.h>

@interface RBProfileVC ()

@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UIView *contentView;
@property(nonatomic, strong) RBProfileResponse *response;

@end

@implementation RBProfileVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"个人资料";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem.alloc initWithImage:[[UIImage imageNamed:@"settingQuestion"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(questionBtnTapped)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:nil forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    _contentView = nil;
    [self.scrollView addView:self.contentView withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    
    __weak typeof(self) weakSelf = self;
    [self.contentView view_addView:({
        UIView *view = [self iconCellWithImgUrl:self.response.data.avatar];
        [view rbAddTapGesWithTarget:self action:@selector(chooseAvatar)];
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"昵称" des:self.response.data.nickname block:^{
        RBChangeNickNameVC *vc = RBChangeNickNameVC.new;
        vc.name = weakSelf.response.data.nickname;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"手机号" des:self.response.data.phone block:^{
        RBChangePhoneVC *vc = RBChangePhoneVC.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self headerView] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(10) bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"姓名" des:self.response.user_detail.name block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"职务" des:self.response.user_detail.job_name block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"手机号码" des:self.response.user_detail.mobile block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    [self.contentView view_addView:[self cellWithTitle:@"医疗机构" des:self.response.user_detail.company_name block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    
    //地址
    NSString *address = [NSString stringWithFormat:@"%@-%@-%@-%@", self.response.user_detail.province, self.response.user_detail.city, self.response.user_detail.district, self.response.user_detail.addr_detail];
    
    [self.contentView view_addView:[self cellWithTitle:@"机构地址" des:address block:^{
        
    }] withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:nil path:@"hu_user_data" post:YES log:YES responseClass:RBProfileResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(RBProfileResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

- (void)uploadAvatarWithImage: (UIImage *)image
{
    __weak typeof(self) weak_self = self;
    [self.defaultHm requestWithParam:^(NSMutableDictionary *dicM) {
        
    } log:YES post:YES cancelLastRequest:NO fileParam:({
        NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
        dicM[kRBRequestMime] = @[@"image/jpeg"];
        dicM[kRBRequestName] = @[@"picurl"];
        dicM[kRBRequestFileName] = @[@"1.jpg"];
        dicM[kRBRequestData] = @[[RanTool dataFromImage:image]];
        dicM;
    }) fileProgress:nil responseClass:RBUploadResponse.class pathName:@"hu_avatar_uploadimg" completion:^(RBUploadResponse *response) {
        if(response.succeed)
        {
            [weak_self changeAvatarWithUrl:response.data];
        }
        else
        {
            [weak_self.view showTip:@"图片上传失败" completion:nil];
        }
    }];
}

- (void)changeAvatarWithUrl: (NSString *)url
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"picurl"] = [@"/" stringByAppendingString:url];
    } path:@"hu_avatar_save" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
        [weak_self getData];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

- (void)questionBtnTapped
{
    
}

- (void)chooseAvatar
{
    __weak typeof(self) weak_self = self;
    [self chooseSingleImageAllowsEditing:YES completion:^(UIImage *image) {
        if(image)
        {
            [weak_self uploadAvatarWithImage:image];
        }
    }];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)contentView
{
    if (!_contentView)
    {
        self.contentView = UIView.new;
    }
    return _contentView;
}

- (UIView *)iconCellWithImgUrl: (NSString *)url
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIImageView *imgView = UIImageView.new;
    [imgView rbSetCornerRadius:Zoom(28)];
    [imgView setImageWithUrl:url placeHolder:nil];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Zoom(15));
        make.bottom.mas_equalTo(Zoom(-15));
        make.left.mas_equalTo(Zoom(18));
        make.width.height.mas_equalTo(Zoom(56));
    }];
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.font = [UIFont systemFontOfSize:Zoom(14)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.text = @"更换头像";
        view;
    });
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(11));
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *arrowImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"mine_arrow_right"]];
    [view addSubview:arrowImgView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(18));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(13));
    }];
    
    return view;
}

- (UIView *)cellWithTitle: (NSString *)title des: (NSString *)des block: (void(^)(void))block
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.font = [UIFont systemFontOfSize:Zoom(14)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.text = title;
        view;
    });
    [view view_addView:titleL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:Zoom(25) left:Zoom(18) right:CGFLOAT_MIN centerX:0];
    
    UIImageView *arrowImgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:@"mine_arrow_right"]];
    [view addSubview:arrowImgView];
    [arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(18));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(7));
        make.height.mas_equalTo(Zoom(13));
    }];
    
    UILabel *desL = ({
        UILabel *view = UILabel.new;
        view.font = [UIFont systemFontOfSize:Zoom(12)];
        view.textColor = [UIColor colorWithHexString:@"#ACACAC"];
        view.text = des;
        view.textAlignment = NSTextAlignmentRight;
        view;
    });
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(arrowImgView.mas_left).offset(-Zoom(8));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(Zoom(200));
    }];

    UIButton *btn = UIButton.new;
    [view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [btn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        if (block)
        {
            block();
        }
    }];
    
    return view;
}

- (UIView *)headerView
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel *titleL = ({
        UILabel *view = UILabel.new;
        view.font = [UIFont boldSystemFontOfSize:Zoom(18)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.text = @"注册资料";
        view;
    });
    [view view_addView:titleL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(30) bottom:Zoom(20) left:Zoom(18) right:CGFLOAT_MIN centerX:0];
    
    return view;
}

@end
